/*
 * @Author: wangcb
 * @Email: wangcbgg@163.com
 * @Date: 2021-08-28 16:08:25
 * @LastEditTime: 2021-10-29 09:48:31
 * 不支持css3(transform)的js滑动方法
 */
import cancelAFrame from "./cancelAFrame";
import requestAFrame from "./requestAFrame";
/**
 * @param {ExtendHTMLElement} element dom元素
 * @param {ISlideOldOptions} options 配置
 */
const slideOld = function (element, options) {
    let requestId;
    element.style.position = 'absolute';
    const format = (prop) => {
        if (options[prop] !== undefined && !isNaN(options[prop])) // @ts-ignore
            options[prop + 'Start'] = parseInt(element.getStyle(prop) || '0', 10);
    };
    format('left');
    format('top');
    format('right');
    format('bottom');
    format('width');
    format('height');
    const move_time = options.duration || 0;
    const startTime = Date.now();
    const setValue = (prop, percent, value) => {
        if (value[prop] !== undefined) { // @ts-ignore
            const start = value[prop + 'Start'] || 0; // @ts-ignore
            const end = value[prop] || 0;
            const now = (end - start) * percent + start; // @ts-ignore
            element.style[prop] = now + 'px';
        }
    };
    const render = () => {
        // 每次变化的时间
        const remaining = Math.max(0, startTime + move_time - Date.now());
        const temp = move_time !== 0 ? remaining / move_time : 0;
        const percent = 1 - temp;
        const stop = () => {
            if (requestId)
                cancelAFrame(requestId);
        };
        const setStyle = (value) => {
            setValue('left', percent, value);
            setValue('top', percent, value);
            setValue('right', percent, value);
            setValue('bottom', percent, value);
            setValue('width', percent, value);
            setValue('height', percent, value);
        };
        // 完成百分比
        if (percent >= 1)
            setStyle(options), stop();
        else
            setStyle(options), requestId = requestAFrame(render);
    };
    render();
};
export default slideOld;
